Title: Jpy Date: 2018-05-01 22:00 Category: Pelican Tags: pelican, python Slug: GradientDescent Author: KeitaW Summary: Test Jupyter notebook conversion
In [9]:
using Plots
using Distributions
gr(fmt="png");
Gradient Descent¶
Gradient descent, aka steepest descent algorithm is the first order optimization algorithm.
In [10]:
f(x, y) = 3x^2 + 5y^2 -6x*y
Out[10]:
In [11]:
function contourf(f, x=linspace(-50, 50, 50), y=linspace(-50, 50, 50))
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
Z = map(f, X, Y)
contour(x, y, Z)
end
Out[11]:
In [12]:
contourf(f)
Out[12]:
In [6]:
using Plots
gr(show = :ijulia) # in IJulia this would be: gr(show = :ijulia)
# x = 0:0.01:2*pi
# for i in 1:200
# display(plot(x, sin.(x + i / 10.0)))
# end
Out[6]:
In [7]:
x = 0:0.01:2*pi
anim = @animate for i=1:100
display(plot(x, sin.(x + i / 10.0)))
end
gif(anim, "/tmp/anim_fps15.gif", fps = 15);
gif(anim, "/tmp/anim_fps30.gif", fps = 30);


In [9]:
display("/tmp/anim_fps15.gif", read("/tmp/anim_fps15.gif"))
In [5]:
a = "~/Dropbox/00_tmp/8.png"
Out[5]:
In [ ]:
dir = "/tmp/anim_fps15.gif"
In [ ]:
using GR
inline("mov")
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
plot(x, sin(x - dt*n))
end
GR.show()
In [1]:
using GR
inline("mov")
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
plot(x, sin(x - dt*n))
end
GR.show()
Out[1]:
In [1]:
using Plots; gr(); default(show=:inline);
for i=1:100
histogram(randn(10000))
sleep(0.2)
end
In [2]:
for i=1:100
histogram(randn(10000))
end
In [1]:
using Plots
default(show=:ijulia)
bins = collect(-3:3); ylims = (0, 400)
p = histogram(randn(1000), bins = bins, ylims = ylims)
for i in 1:10
h = histogram(randn(1000), bins = bins, ylims = ylims);
p[1] = h.series_list[1][:x], h.series_list[1][:y]
sleep(0.2)
p
end
In [ ]:
using Plots
gr(fmt="png")
x = linspace(0, 2*pi, 100) |> collect
dt, nsteps = 0.03, 100
for n = 1:nsteps
IJulia.clear_output(true)
Plots.plot(x, sin(x - n*dt)) |> display
end
In [13]:
l = @layout([[a;b] c])
p = plot(plot([sin,cos],1,leg=false),
scatter([atan,cos],1,leg=false),
plot(log,1,xlims=(1,10π),ylims=(0,5),leg=false),layout=l)
anim = Animation()
for x = linspace(1,10π,100)
plot(push!(p,x,Float64[sin(x),cos(x),atan(x),cos(x),log(x)]))
frame(anim)
end
In [55]:
plot(
contourf(f),
contourf(f),
contourf(f),
contourf(f)
)
Out[55]:
In [36]:
x = y = linspace(-50, 50, 50)
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
f(x, y) = 3x^2 + 5y^2 -6x*y
Z = map(f, X, Y)
contour(x, y, Z)
Out[36]:
In [19]:
x = 1:0.5:20
y = 1:0.5:10
f(x,y) = begin
(3x + y ^ 2) * abs.(sin.(x) + cos.(y))
end
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
p1 = contour(x,y,f,fill=true)
p2 = contour(x,y,Z)
plot(p1,p2)
Out[19]:
In [11]:
xx=linspace(-1,1,100)
yy=xx
ff(x,y)=pdf(MvNormal([0.,0],[3. 2;2 3]),[x,y])
gg=[ff(x,y)::Float64 for x=xx,y=yy]
qlevels=[0,0.1,0.2,1]
levels=quantile(vec(gg),qlevels)
contour(xx,yy,gg,fill=true,levels=levels,c=:heat) #figure 4
contour(xx,yy,gg,fill=true,levels=levels,c=ColorGradient(:heat,qlevels)) #figure 5
In [15]:
ColorGradient(:heat, qlevels)
In [21]:
?surface
Out[21]:
In [19]:
f.(x, y)
Out[19]:
In [ ]:
k